package com.mianyang.zhangsan.plugin.export;


import com.mianyang.zhangsan.common.util.StringUtils;

import java.io.*;
import java.util.Map;
import java.util.UUID;

/**
 * @author jiangjr
 * @date 2022-10-13
 */
public abstract class AbstractTemplate {
    protected Map<String, Object> variable;

    /**
     * 填充模板
     */
    public abstract void fill();

    /**
     * 生成模板
     */
    public abstract OutputStream generator();

    public String getFilePath(String outPath, String suffix) {
        if (StringUtils.isEmpty(outPath)) {
            return UUID.randomUUID() + suffix;
        }
        return outPath + File.separator + UUID.randomUUID() + suffix;
    }

    public void setVariable(Map<String, Object> variable) {
        this.variable = variable;
    }

    public Map<String, Object> getVariable() {
        return this.variable;
    }

    public OutputStream build(Map<String, Object> variable) {
        setVariable(variable);
        this.fill();
        return this.generator();
    }
}
